package com.ocom.payconfig.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityCloudComputingInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.payconfig.cloudcomputing.CloudComputingVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoCloudComputing;
import com.ocom.payconfig.service.CloudComputingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class CloudComputingServiceImpl extends ServiceImpl<IDaoCloudComputing, EntityCloudComputingInfo> implements CloudComputingService {

    @Autowired
    private CloudComputingService cloudComputingService;

    @Autowired
    private IDaoCloudComputing iDaoCloudComputing;


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster=MasterSlaveEnum.SLAVE)
    public Result queryPersonnelInfoPage(Long comId,CloudComputingVo cloudComputingVo) {
        if (ObjectUtil.isEmpty(cloudComputingVo.getPage())) {
            cloudComputingVo.setPage(1);
        }
        if (ObjectUtil.isEmpty(cloudComputingVo.getSize())) {
            cloudComputingVo.setSize(10);
        }

        PageHelper.startPage(cloudComputingVo.getPage(),cloudComputingVo.getSize());

        List<CloudComputingVo> list = iDaoCloudComputing.selectPersonnelInfoWithConditions(cloudComputingVo);
        PageInfo<CloudComputingVo> pageInfo = new PageInfo<>(list);

        if (ObjectUtil.isEmpty(pageInfo.getList())) {
            return ResultUtil.success();
        }

        if (ObjectUtil.isEmpty(cloudComputingVo.getPId())) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", pageInfo.getList());
            resultMap.put("total", pageInfo.getTotal());
            return ResultUtil.success(resultMap);
        }
        CloudComputingVo computingVo = pageInfo.getList().get(0);
        return ResultUtil.success(computingVo);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster=MasterSlaveEnum.MASTER)
    public Result modifyCloudComputingInfo(Long comId,CloudComputingVo cloudComputingVo) {

        LambdaUpdateWrapper<EntityCloudComputingInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EntityCloudComputingInfo::getComId, cloudComputingVo.getComId())
                .eq(EntityCloudComputingInfo::getPId, cloudComputingVo.getPId());

        updateWrapper.set(ObjectUtil.isNotEmpty(cloudComputingVo.getPId()), EntityCloudComputingInfo::getPId, cloudComputingVo.getPId())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getUserName()), EntityCloudComputingInfo::getUsername, cloudComputingVo.getUserName())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getPassword()), EntityCloudComputingInfo::getPassword, cloudComputingVo.getPassword())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getUseInfo()), EntityCloudComputingInfo::getUseInfo, cloudComputingVo.getUseInfo())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getCustomerInfo()), EntityCloudComputingInfo::getCustomerInfo, cloudComputingVo.getCustomerInfo())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getEndTime()), EntityCloudComputingInfo::getEndTime, cloudComputingVo.getEndTime())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getBalance()), EntityCloudComputingInfo::getBalance, cloudComputingVo.getBalance())
                .set(ObjectUtil.isNotEmpty(cloudComputingVo.getRemark()), EntityCloudComputingInfo::getRemark, cloudComputingVo.getRemark());

        if (ObjectUtil.isNotEmpty(cloudComputingVo.getStatus())) {
            updateWrapper.set(ObjectUtil.isNotEmpty(cloudComputingVo.getUpdateUser()), EntityCloudComputingInfo::getCheckUser, cloudComputingVo.getUpdateUser())
                    .set(EntityCloudComputingInfo::getStatus, cloudComputingVo.getStatus())
                    .set(EntityCloudComputingInfo::getCheckTime, new Date());
        } else {
            updateWrapper.set(ObjectUtil.isNotEmpty(cloudComputingVo.getUpdateUser()), EntityCloudComputingInfo::getUpdateUser, cloudComputingVo.getUpdateUser())
                    .set(EntityCloudComputingInfo::getUpdateTime, new Date());

        }

        boolean flag = cloudComputingService.update(updateWrapper);
        if (!flag) {
            EntityCloudComputingInfo entityCloudComputingInfo = new EntityCloudComputingInfo();
            entityCloudComputingInfo.setComId(cloudComputingVo.getComId());
            entityCloudComputingInfo.setPId(cloudComputingVo.getPId());
            entityCloudComputingInfo.setUsername(cloudComputingVo.getUserName());
            entityCloudComputingInfo.setPassword(cloudComputingVo.getPassword());
            entityCloudComputingInfo.setUseInfo(cloudComputingVo.getUseInfo());
            entityCloudComputingInfo.setCustomerInfo(cloudComputingVo.getCustomerInfo());
            entityCloudComputingInfo.setEndTime(cloudComputingVo.getEndTime());
            entityCloudComputingInfo.setBalance(cloudComputingVo.getBalance());
            entityCloudComputingInfo.setStatus((byte) (cloudComputingVo.getStatus()==null?0:cloudComputingVo.getStatus()));
            entityCloudComputingInfo.setRemark(cloudComputingVo.getRemark());
            entityCloudComputingInfo.setUpdateTime(new Date());

            try {
                cloudComputingService.save(entityCloudComputingInfo);
            } catch (Exception e) {
                log.info("异常信息{}",e.getMessage());
                return ResultUtil.error(ResultEnum.Err_2016);
            }

        }
        return ResultUtil.success();
    }
}
